<?php
/*
 * activity_collection.php
 *
 * Copyright (c) 2007 Marcelo Lira dos Santos
 *
 * Author: Marcelo Lira dos Santos <setanta@gmail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

require_once 'activity.php';
require_once 'DBManager.php';

class ActivityCollection {
    var $dbconnection;

    function ActivityCollection() {
        $this->__construct();
    }

    function __construct() {
        $dbconnection = new DBManager();
    }

    function get_list() {
        $dbconnection = new DBManager();

        $query = "SELECT id,titulo,autor,conteudo,".
                 "(SELECT nome FROM soft_softwares WHERE id=software) ".
                 "FROM soft_atividades";

        $dbconnection->query($query);
        $dbconnection->close();

        $activities = array();
        for($i = 0; $in = $dbconnection->next_record(); $i++) {
            $row = array($in[id], $in[titulo], $in[autor], $in[conteudo],
                    $in[4]);
            array_push($activities, $row);
        }

        return $activities;
    }

    function get_some_activities($id_list) {
        $selected = array();

        foreach($id_list as $id) {
            array_push($selected, $this->get_activity($id));
        }

        return $selected;
    }

    function get_all_activities() {
        $dbconnection = new DBManager();

        $query = "SELECT * FROM soft_atividades";

        $dbconnection->query($query);
        $dbconnection->close();

        $activities = array();
        for($i = 0; $in = $dbconnection->next_record(); $i++)
            array_push($activities, $this->assemble_activity($in));

        if ($i = 0)
            $activities= false;

        return $activities;
    }

    function get_activity($id) {
        $dbconnection = new DBManager();

        $query = "SELECT titulo,autor,conteudo,faixa,duracao,objetivos," .
                 "procedimentos,avaliacao,software FROM soft_atividades " .
                 "WHERE id='$id'";

        $dbconnection->query($query);
        $dbconnection->close();

        $in = $dbconnection->next_record();

        if ($in) {
            $activity = $this->assemble_activity($in);
        } else
            $activity = false;

        return $activity;
    }

    function assemble_activity($in) {
        $activity = new Activity($in[0], $in[1], $in[2], $in[8]);
        $activity->age = $in[faixa];
        $activity->duration = $in[duracao];
        $activity->goals = $in[objetivos];
        $activity->procedures = $in[procedimentos];
        $activity->evaluation = $in[avaliacao];

        // Corrigir!!!
        $activity->category = 1;

        return $activity;
    }
}
?>
